/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @AppTitleBar.java  2014-9-9 上午10:22:53 - Carson
 * @author YanXu
 * @email:981385016@qq.com
 * @version 1.0
 */

package com.android.tiango.baba.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseApplication;

public class AppTitleBar extends RelativeLayout {

	private FrameLayout mTitleBack;
	private ImageView mImageBack;
	private TextView mTitleName;
	private TextView mTvmoreTextView;

	private FrameLayout mTitleMore;
	public TextView mImageMore;
	private ImageView mMoreleftImageView;
	private RelativeLayout mTitlebarRelativeLayout;
	private ImageView mTitlebartitlepic;

	public AppTitleBar(Context context) {
		super(context);
		initViewLayout(context);
	}

	public AppTitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initViewLayout(context);
	}

	public AppTitleBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initViewLayout(context);
	}

	private void initViewLayout(Context context) {
		LayoutInflater.from(context).inflate(R.layout.layout_apptitle_bar, this, true);
		mTitleBack = (FrameLayout) findViewById(R.id.titlebar_back);
		mImageBack = (ImageView) findViewById(R.id.image_back);
		mTitleName = (TextView) findViewById(R.id.titlebar_title);
		mTitleMore = (FrameLayout) findViewById(R.id.titlebar_more);
		mImageMore = (TextView) findViewById(R.id.image_more);
		mTitlebarRelativeLayout = (RelativeLayout) findViewById(R.id.id_titlebar);
		mMoreleftImageView = (ImageView) findViewById(R.id.image_moreleft);
		mTvmoreTextView = (TextView) findViewById(R.id.tv_more);
		mTitlebartitlepic = (ImageView) findViewById(R.id.titlebar_titlepic);
	}

	/**
	 * the left button's onclick listener
	 */
	public void setBackOnClickListener(OnClickListener l) {
		if (!mTitleBack.isShown()) {
			mTitleBack.setVisibility(View.VISIBLE);
		}
		mTitleBack.setOnClickListener(l);
	}

	/**
	 * the right button's onclick listener
	 */
	public void setMoreImageOnClickListener(OnClickListener l) {
		// mTitleMore.setOnClickListener(l);
		if (!mTitleMore.isShown()) {
			mTitleMore.setVisibility(View.VISIBLE);
		}
		mTitleMore.setOnClickListener(l);
	}

	public void setMoreTextOnClickListener(OnClickListener l) {
		// mTitleMore.setOnClickListener(l);
		if (!mTvmoreTextView.isShown()) {
			mTvmoreTextView.setVisibility(View.VISIBLE);
		}
		mTvmoreTextView.setOnClickListener(l);
	}

	public void setTitle(CharSequence title) {
		if (mTitleName != null) {
			mTitlebartitlepic.setVisibility(View.GONE);
			mTitleName.setVisibility(View.VISIBLE);
			mTitleName.setText(title);
		}
	}

	public void setTitleBg(int reId) {
		if (mTitleName != null) {
			mTitlebartitlepic.setVisibility(View.VISIBLE);
			mTitleName.setVisibility(View.GONE);
			mTitlebartitlepic.setBackgroundResource(reId);
		}
	}

	public void setBackIcon(int resid) {
		if (mImageBack != null) {
			mTitleBack.setVisibility(View.VISIBLE);
			mImageBack.setImageResource(resid);
		}
	}

	public void setMoreIcon(int resid) {
		if (mImageMore != null) {
			mImageMore.setVisibility(View.VISIBLE);
			mTvmoreTextView.setVisibility(View.GONE);
			mImageMore.setBackgroundResource(resid);
		}
	}

	public void setMoreText(String msg) {
		if (mTvmoreTextView != null) {
			mTvmoreTextView.setVisibility(View.VISIBLE);
			mImageMore.setVisibility(View.GONE);
			mTvmoreTextView.setText(msg);
		}
	}
	public void setMoreTextBg(int resId) {
		if (mTvmoreTextView != null) {
			mTvmoreTextView.setVisibility(View.VISIBLE);
			mImageMore.setVisibility(View.GONE);
			mTvmoreTextView.setBackgroundResource(resId);
		}
	}
	public void setMoreLeftIcon(int resid) {
		if (mMoreleftImageView != null) {
			mMoreleftImageView.setVisibility(View.VISIBLE);
			mMoreleftImageView.setImageResource(resid);
		}
	}

	public void setMoreLeftIconListener(OnClickListener l) {
		if (!mMoreleftImageView.isShown()) {
			mMoreleftImageView.setVisibility(View.VISIBLE);
		}
		mMoreleftImageView.setOnClickListener(l);
	}

	public void setImageBackGround(int id) {
		if (null != mTitlebarRelativeLayout) {
			mTitlebarRelativeLayout.setBackgroundResource(id);
		}
	}

	@SuppressLint("NewApi")
	public void setImageNotBackGround() {
		mTitlebarRelativeLayout.setBackground(null);
	}

	public void setColorBackGround(int id) {
		if (null != mTitlebarRelativeLayout) {
			mTitlebarRelativeLayout.setBackgroundColor(BaBaseApplication.getAppContext()
					.getResources().getColor(id));
		}
	}
}
